package com.neuedu.web.utils;

import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 金山
 * 项目：javaweb
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2024/11/1816:58
 */
public class BeanConvertUtils {


    /**
     * 从一个类中 复制属性值到另一个类对象中
     * @param clazz
     * @param obj
     * @return
     * @param <T>
     */
    public static <T> T conver(Class<T> clazz ,Object obj){

        Object dest = null;
        try {

            dest = clazz.newInstance();
            BeanUtils.copyProperties(dest,obj);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        }

        return (T) dest;
    }


    /**
     * PO转换成 VO
     * @param list
     * @param targetClass
     * @return
     */
    public static  List po2Vo(List list , Class targetClass){
        Object result = list.stream().map(po -> {
            return BeanConvertUtils.conver(targetClass, po);
        }).collect(Collectors.toList());

        return (List) result;
    }


}
